package com.nelson.transform

import com.android.build.api.transform.*
import com.android.build.gradle.internal.pipeline.TransformManager
import com.android.utils.FileUtils
import org.apache.commons.codec.digest.DigestUtils
import org.gradle.api.Project

class InjectTransform extends Transform {

    Project project;
    InjectTransform(Project project){
        this.project = project
    }

    @Override
    String getName() {
        return "InjectTransform"
    }

    @Override
    Set<QualifiedContent.ContentType> getInputTypes() {
        return TransformManager.CONTENT_CLASS
    }

    @Override
    Set<? super QualifiedContent.Scope> getScopes() {
        return TransformManager.SCOPE_FULL_PROJECT
    }

    @Override
    boolean isIncremental() {
        return true
    }

    @Override
    void transform(Context context, Collection<TransformInput> inputs, Collection<TransformInput> referencedInputs, TransformOutputProvider outputProvider, boolean isIncremental) throws IOException, TransformException, InterruptedException {
        println '-------------- transform 开始 ---------------'

    /*    inputs.each { input ->
            // DirInput 处理
            // 遍历文件夹
            input.directoryInputs.each {directoryInput->
                // 注入代码
                InjectByJavassist.injectToast(directoryInput.file.absolutePath, project)

                // 获取输出目录
                def dest = outputProvider.getContentLocation(directoryInput.name, directoryInput.contentTypes, directoryInput.scopes, Format.DIRECTORY)
                println "directory output dest: $dest.absolutePath"
                // 将 input 的目录复制到 output 指定目录
                FileUtils.copyDirectory(directoryInput.file, dest)
            }

            // JarInput 处理
            input.jarInputs.each {jarInput->
                def jarName = jarInput.name
                println "jar: $jarInput.file.absolutePath"
                def md5Name = DigestUtils.md5Hex(jarInput.file.absolutePath)
                if(jarName.endsWith(".jar")) {
                    jarName = jarName.substring(0, jarName.length() -4)
                }

                def dest = outputProvider.getContentLocation(jarName + md5Name, jarInput.contentTypes, jarInput.scopes, Format.JAR)
                println "jar output dest: $dest.absolutePath"
                FileUtils.copyFile(jarInput.file, dest)
            }
        }*/

        inputs.each { input ->
            // DirInput 处理
            // 遍历文件夹
            input.directoryInputs.each {directoryInput->
                directoryInput.changedFiles.each { changedFileEntry->
                    def status = changedFileEntry.value


                }
                // 注入代码
                InjectByJavassist.injectToast(directoryInput.file.absolutePath, project)

                // 获取输出目录
                def dest = outputProvider.getContentLocation(directoryInput.name, directoryInput.contentTypes, directoryInput.scopes, Format.DIRECTORY)
                println "directory output dest: $dest.absolutePath"
                // 将 input 的目录复制到 output 指定目录
                FileUtils.copyDirectory(directoryInput.file, dest)
            }

            // JarInput 处理
            input.jarInputs.each {jarInput->
                def jarName = jarInput.name
                println "jar: $jarInput.file.absolutePath"
                def md5Name = DigestUtils.md5Hex(jarInput.file.absolutePath)
                if(jarName.endsWith(".jar")) {
                    jarName = jarName.substring(0, jarName.length() -4)
                }

                def dest = outputProvider.getContentLocation(jarName + md5Name, jarInput.contentTypes, jarInput.scopes, Format.JAR)
                println "jar output dest: $dest.absolutePath"
                FileUtils.copyFile(jarInput.file, dest)
            }
        }

        println "-------------- transform 结束 -------------"
    }
}